package com.excel.tools;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.ss.usermodel.Cell;

import com.excel.vo.VO;

public class Reflector {
	
	public static boolean setter(VO vo, String name, Cell cell, String datePattern) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchFieldException, ParseException {
		if (name == null) {
			return false;
		}
		String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
		Field field = vo.getClass().getDeclaredField(name); // 属性
		field.setAccessible(true); // 设置可访问性
		String type = field.getType().getSimpleName(); // 属性类型名称
		Object value = null;
		if ("boolean".equals(type)) {
			// 逻辑值
			field.setBoolean(vo, CellFormatter.getBooleanCellValue(cell));
			return true;
		} else if ("double".equals(type)) {
			// double
			field.setDouble(vo, CellFormatter.getNumericCellValue(cell));
			return true;
		} else if ("float".equals(type)) {
			// float
			field.setFloat(vo, ((Double)CellFormatter.getNumericCellValue(cell)).floatValue());
			return true;
		} else if ("int".equals(type)) {
			// 整型
			field.setInt(vo, (int)CellFormatter.getNumericCellValue(cell));
			return true;
		} else if ("Date".equals(type)) {
			// 日期
			value = CellFormatter.getDateCellValue(cell, datePattern);
		} else if ("String".equals(type)) {
			// 字符串
			value = CellFormatter.getStringCellValue(cell);
		} else if ("Integer".equals(type)) {
			// 整型封装类
			value = (int)CellFormatter.getNumericCellValue(cell);
		} else if ("Double".equals(type)){
			// Double封装类
			value = CellFormatter.getNumericCellValue(cell);
		} else if ("Float".equals(type)) {
			// Float封装类
			value = ((Double)CellFormatter.getNumericCellValue(cell)).floatValue();
		} else if ("Boolean".equals(type)) {
			// Boolean封装类
			value = CellFormatter.getBooleanCellValue(cell);
		} else {
			value = CellFormatter.getStringCellValue(cell);
		}
		Method method = vo.getClass().getMethod(methodName, value.getClass());
		method.invoke(vo, value);
		return true;
	}
	
	public static boolean getter(VO vo, String name, Cell cell, String datePattern) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
		if (name == null) {
			return false;
		}
		String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
		Method method = vo.getClass().getMethod(methodName);
		Object value =  method.invoke(vo);
		String type = vo.getClass().getDeclaredField(name).getType().getSimpleName(); // 属性类型
		if ("string".equalsIgnoreCase(type)) {
			// 字符串
			cell.setCellType(Cell.CELL_TYPE_STRING);
			cell.setCellValue(value.toString());
		} else if ("boolean".equalsIgnoreCase(type)) {
			// 逻辑值
			cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
			cell.setCellValue(Boolean.valueOf(value.toString()));
		} else if ("double".equalsIgnoreCase(type) || "float".equalsIgnoreCase(type)) {
			// 数值
			cell.setCellType(Cell.CELL_TYPE_NUMERIC);
			cell.setCellValue(Double.valueOf(value.toString()));
		} else if ("int".equals(type) || "Integer".equalsIgnoreCase(type)) {
			// 整数
			cell.setCellType(Cell.CELL_TYPE_STRING);
			cell.setCellValue(value.toString());
		} else if ("date".equalsIgnoreCase(type)) {
			// 日期
			cell.setCellType(Cell.CELL_TYPE_STRING);
			cell.setCellValue(new SimpleDateFormat(datePattern).format((Date)value));
		} else {
			cell.setCellType(Cell.CELL_TYPE_STRING);
			cell.setCellValue(value.toString());
		}
		return true;
	}
}
